package com.jrj.service;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.stereotype.Service;

import com.jrj.dao.CsdnDao;
import com.jrj.domain.Csdn;

/**
 *@author zhiming.li
 *@version createTime：2011-12-22 上午09:00:58
 */
@Service
public class CsdnService extends BaseService{

	@Autowired
	private CsdnDao csdnDao;
	
	public Integer addCsdn(Csdn csdn){
		Integer i = null;
		if(null!=csdn){
			i = csdnDao.addCsdn(csdn);
		}
		return i;
	}
	
	/**
	 * jdbcTemple
	 * @param csdn
	 * @return
	 */
	public void addCsdn1( List<Csdn> listCsdn){
		
		//System.out.println(this.getJdbcTemplate()+"----");
		
		
		final List<Csdn> tempList = listCsdn;
		String sql = "insert into csdn(userName,passWord,email) values(?,?,?)";
		this.jdbcTemplate.execute(sql, new PreparedStatementCallback() {
			  		
			@Override
			public Object  doInPreparedStatement(PreparedStatement ps)
					throws SQLException, DataAccessException {
				 int length = tempList.size();  
	               ps.getConnection().setAutoCommit(false);  
	               for(int i=0;i<length;i++){  
	                    ps.setString(1, tempList.get(i).getUserName());  
	                    ps.setString(2,tempList.get(i).getPassWord());  
	                    ps.setString(3,tempList.get(i).getEmail());  
	                    ps.addBatch();  
	                }  
	                Object o = ps.executeBatch();  
	                ps.getConnection().commit();  
	                ps.getConnection().setAutoCommit(true);  
				return o;
			}
		});
		
		
		
//		try {
//			this.getJdbcTemplate().batchUpdate(sql,new BatchPreparedStatementSetter() {
//				
//				@Override
//				public void setValues(PreparedStatement ps, int i) throws SQLException {
//					   ps.setString(1, tempList.get(i).getUserName());
//	                   ps.setString(2, tempList.get(i).getPassWord());
//	                   ps.setString(3, tempList.get(i).getEmail());
//				}
//				
//				@Override
//				public int getBatchSize() {
//					return tempList.size();
//				}
//			});
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		
		
		//return tempList;
	}
	
}
